package com.towa.ninemanmorris;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.os.AsyncTask;
import android.util.Log;

public class GroupTasker extends AsyncTask<String, Void, ArrayList<String>> {

	@Override
	protected ArrayList<String> doInBackground(String... params) {
		Socket socket = null;
		PrintWriter pw;
		BufferedReader br;
		ArrayList<String> returnArray = new ArrayList<String>();
		try {
			socket = new Socket(NineManMorrisConstants.INET_ADDRESS, NineManMorrisConstants.INET_PORT);
			pw = new PrintWriter(socket.getOutputStream());
			Log.i("GrouPTasker", params[0]);
			pw.println(params[0]);
			pw.flush();
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String response = br.readLine();
			String[] splits = response.split("::");
			int groupsLength = Integer.parseInt(splits[1]);
			for (int i = 0; i < groupsLength; i++) {
				returnArray.add(br.readLine());
			}
		} catch (UnknownHostException e) {
			Log.e("GroupTasker", e.toString());
		} catch (IOException e) {
			Log.e("GroupTasker", e.toString());
		}
		return returnArray;
	}

}
